package com.opengms.HydroModelsIntegration.Dao;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Scanner;

/**
 * @Author: David.Xiao
 * @Date: 2019/1/15 10:07
 * @Description:
 */
public class FileHelper {
    public static String readXml(File xmlFile)
    {
        SAXReader reader = new SAXReader();
        Document document;
        String xmlStr = null;
        try {
            document = reader.read(xmlFile);
            xmlStr = document.asXML();

        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return xmlStr;
    }
    public static boolean pathExists(File file)
    {
        if (file.exists() && !file.isDirectory())
        {
            return false;
        }
        else {
            System.out.println("Path not exist.Creating one...");
            file.mkdir();
        }
        return true;
    }
    public static String readJson(File file)
    {
        Scanner scanner = null;
        StringBuilder builder = new StringBuilder();
        try {
            scanner = new Scanner(file, "utf-8");
            while (scanner.hasNextLine()) {
                builder.append(scanner.nextLine());
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            if (scanner!=null)
            {
                scanner.close();
            }
        }
        return builder.toString();
    }
}
